﻿Imports VSModel
Partial Class Control_WhatCookToDay_SearchPorlet
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            LoadMeterial()
            LoadMethod()
            Dim arrSearch() As String
            arrSearch = Session("search")
            If (Not arrSearch Is Nothing) Then
                txtRecipename.Text = arrSearch(0).ToString
                ddlMeterial.SelectedIndex = ddlMeterial.Items.IndexOf(ddlMeterial.Items.FindByValue(arrSearch(1).ToString))
                ddlMethod.SelectedIndex = ddlMethod.Items.IndexOf(ddlMethod.Items.FindByValue(arrSearch(2).ToString))
                txtdatepicker.Text = arrSearch(3).ToString
            End If
            btnSearch.Attributes.Add("OnClick", "return CheckConditionSearch()")
        End If
    End Sub
#Region "Sub and function"
    'Load danh sach nguyen vat lieu
    Sub LoadMeterial()
        Using ssmdata As New VSEntities
            Dim obj = (From m In ssmdata.Materials Order By m.Title Ascending Select m).ToList
            With ddlMeterial
                .DataTextField = "Title"
                .DataValueField = "Id"
                .DataSource = obj
                .DataBind()
            End With
            Dim item As New ListItem("Nguyên liệu", "0")
            ddlMeterial.Items.Insert(0, item)
        End Using
    End Sub
    'Load danh sach cach thuc hien
    Sub LoadMethod()
        Using ssmdata As New VSEntities
            Dim obj = (From m In ssmdata.WayToCooks Order By m.Title Ascending Select m).ToList
            With ddlMethod
                .DataTextField = "Title"
                .DataValueField = "Id"
                .DataSource = obj
                .DataBind()
            End With
            Dim item As New ListItem("Cách thực hiện", "0")
            ddlMethod.Items.Insert(0, item)
        End Using
    End Sub
#End Region

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim arrSearch() As String = {txtRecipename.Text.Trim, ddlMeterial.SelectedValue, ddlMethod.SelectedValue, txtdatepicker.Text.Trim}
        Session("search") = arrSearch
        Response.Redirect("~/" & Page.RouteData.Values("MenuId") & "/nau-gi-hom-nay/ket-qua-tim-kiem.html")
    End Sub
End Class
